package com.fhz.day006.sp

import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.edit
import androidx.lifecycle.ViewModelProvider
import com.fhz.day006.R
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main3.*

class Main3Activity : AppCompatActivity() {


    lateinit var sp:SharedPreferences

    lateinit var viewModel: Main3ViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main3)

        sp = getSharedPreferences("main3", MODE_PRIVATE)
        val i = sp.getInt("count", 0)

        viewModel = ViewModelProvider(this,MyFactory(i)).get(Main3ViewModel::class.java)

        tv3.text = viewModel.num.toString()

        btn3.setOnClickListener {
            viewModel.num++
            tv3.text = viewModel.num.toString()
        }

    }

    override fun onPause() {
        super.onPause()
        sp.edit {
            putInt("count",viewModel.num)
        }

    }
}
